
<?
$authoritys=array	(
					"admin"=>";admin;",
					"contact"=>";contact;",
					);
if(isset($_SESSION[ "label"		])){
	$labels=$_SESSION[ "label"		];
}else{
	$labels=array	(
					"bnone"=>"none",
					"bred"=>"red",
					"bblue"=>"blue",
					"bgreen"=>"green",
					"bpurple"=>"purple",
					);
}
require ('../Model/_ContactModel.php');
$page=isset($_REQUEST["page"])?$_REQUEST["page"]:1;
$n=isset($_SESSION[ "numrow"		])?$_SESSION[ "numrow"		]:10;
$trash=isset($_REQUEST["trash"])?true:false;
$contacs=new _ContactModule();
$data=$contacs->getContacts($n,$page-1,$trash);
$numrow=$contacs->getContacts_numrows($trash);
$numpage= ($numrow%$n==0)?(int)($numrow/$n):((int)($numrow/$n)+1);

?>
<div class="title_wrapper">
	<h2>Main Section Title</h2>
	<a href="#"><span>Open Site</span></a>
</div>
<!--[if !IE]>start section<![endif]-->
<div class="section">
	
	<h3>Contact US</h3>
	
	<!--[if !IE]>start section content<![endif]-->
	<div class="section_content">
		
		<div class="tabs">
			<ul>
				<li><a href="?" class="<?=$trash?"":"active"?>"><span><span>All Contact</span></span></a></li>
				<li><a href="?trash=true" class="<?=$trash?"active":""?>"><span><span>Trash</span></span></a></li>
			</ul>
		</div>
		
		
		
		<p>
			<a href="?tool=setting"><span><span>Click here to setting contact label and rows display.</span></span></a>
		</p>
		
		<?php 
		$admin_author = 	strpos($_SESSION[ "account_authorityniv"],$authoritys["admin"]) ;
		$contact_author=	strpos($_SESSION[ "account_authorityniv"],$authoritys["contact"]);
		if($admin_author===FALSE && $contac_author===False){
			emptydata();
		}else{
		?>
		<div  class="product_list">
			<!--[if !IE]>start table_wrapper<![endif]-->
			<div class="table_wrapper">
				<div class="table_wrapper_inner">
				<table cellpadding="0" cellspacing="0" width="100%">
					<tbody>
					<tr>
						<th class="no"><span><span><a href="#">No.</a></span></span></th>
						<th class="photo"><span><span>Name</span></span></th>
						<th class="name order"><span><span><a href="#">Email</a> <a href="#" class="desc">descendent order</a></span></span></th>
						<th class="price"><span><span><a href="#">Type</a></span></span></th>
						<th class="insert"><span><span><a href="#">Insert</a></span></span></th>
						<th class="move"><span><span>Label</span></span></th>
						<th class="actions"><span><span>Actions</span></span></th>
					</tr>
					<?
					if($data!=null){
					foreach($data as $row){
						if(isset($row["label"])){
							$arr=explode(";", $row["label"]);
							$label=isset($arr[0])?$arr[0]:null;
							$er=isset($arr[0])?$arr[1]:null;
							$au=isset($arr[0])?$arr[2]:null;
						}else{
							$label=null;
							$er=null;
							$au=null;
						}
					?>
					<tr class="data-row<?=$row["isnew"]?" _new":""?> rowid-<?=$row["key"]?>">
						<td style="width: 21px;">
							<?=$row["key"]?>
						</td>
						<td >
							<?=$row["name"]?>
						</td>
						<td style="">
							<?=$row["email"]?>
						</td>
						<td>
							<?=$row["type"]?>
						</td>
						<td style="">
							<?
							$_time=new DateTime($row["insert"]);
            				if($row["dateiff"]==0)echo "Today at ".($_time->format(' H:i:s'));
							elseif($row["dateiff"]==1)echo "Yesterday at ".($_time->format(' H:i:s'));
							elseif($row["dateiff"]<3)echo $row["dateiff"]." day ago at ".($_time->format(' H:i:s'));
							else echo ($_time->format('y-m h:i:s A'));
							?>
						</td>
						<td style="width: 64px;">
							<div class="move_menu">
								<div class="change label <?=isset($label)?$label:"bnone"?>" contactid="<?=$row["key"]?>"><div class=""></div><span style="font-weight:normal;"><?=isset($label)?$labels[$label]:$labels["bnone"]?></span></div>
<!--								<ul>-->
<!--									<li><a href="#" class="move1">1</a></li>-->
<!--									<li><a href="#" class="move2">2</a></li>-->
<!--									<li><a href="#" class="move3">3</a></li>-->
<!--									<li><a href="#" class="move4">4</a></li>-->
<!--								</ul>-->
							</div>
						</td>
						<td style="width: 120px;">
							<div class="actions_menu">
								<ul>
									<li><a class="details" contactid="<?=$row["key"]?>"  href="javascript:void(0);">Details</a></li>
									<li><a class="edit" href="#">Edit</a></li>
									<?php 
									if($trash){
									?>
									<li><a class="undelete" href="javascript:void(0);" title="<?=$row["key"]?>">Restore</a></li>
									<?php 
									}else{
									?>
									<li><a class="delete" href="javascript:void(0);" title="<?=$row["key"]?>">Delete</a></li>
									<?php 
									}
									?>
								</ul>
							</div>
						</td>
					</tr>
					<tr>
						<td colspan="7" style="display:none;"></td>
					</tr>
					<tr class="viewingbottom rowdetail-<?=$row["key"]?>">
						<td colspan="5" style="">
							<div>
							<table class="nonetable">
								<tr>
									<td>Name :</td><td><?=$row["name"]?></td><td>Email :</td><td><?=$row["email"]?></td><td>Phone :</td><td><?=$row["phone"]?></td>
								</tr>
								<tr>
									<td>Country :</td><td><?=$row["country"]?></td><td>Address :</td><td><?=$row["address"]?></td><td>Type :</td><td><?=$row["type"]?></td>
								</tr>
								<tr>
									<td colspan="6">Content :</td>
								</tr>
								<tr>
									<td colspan="6" id="ctct"><pre style="white-space: pre-wrap;       /* css-3 */
 white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
 white-space: -pre-wrap;      /* Opera 4-6 */
 white-space: -o-pre-wrap;    /* Opera 7 */
 word-wrap: break-word;  "><?=$row["contentencode"]?></pre></td>
								</tr>
							</table>
							</div>
						</td>
						<td colspan="2" style="vertical-align:top;">
							Insert : <?=$row["insert"]?><br/>
							Label : <?=isset($er)?$er." set label is ":""?><span class="label-<?=$row["key"]?> <?=isset($label)?$label:"bnone"?>" style="font-weight:normal;"><?=isset($label)?$labels[$label]:$labels["bnone"]?></span><br/>
							Viewers : <?=$row["viewers"]?>
							<?=(isset($row["delete"])?"<br/>Delete : ".$row["delete"]:"")?>
							<?=(isset($row["log"])?"<br/>Log :<pre>".$row["log"]."</pre>":"")?>
						</td>
					</tr>
					<?
					}
					}
					?>
					
					
				</tbody></table>
				</div>
			</div>
			<!--[if !IE]>end table_wrapper<![endif]-->
			
			<script type="text/javascript">
				$(".change.label").click(
					function(){
						var obj=$(this).find("div:first");
						var id=$(this).attr("contactid");
						var _class=$(this).attr("class");
						if(_class.indexOf("bred")>=0){
							$(this).removeClass("bred");
							$(this).addClass("bblue");
							$(obj).next().html("<?=$labels["bblue"]?>");
							$(".label-"+id).html("<?=$labels["bblue"]?>");
							$(".label-"+id).removeClass("bred");
							$(".label-"+id).addClass("bblue");
							changelabel(id,"bblue");
						}else if(_class.indexOf("bblue")>=0){
							$(this).removeClass("bblue");
							$(this).addClass("bgreen");
							$(".label-"+id).removeClass("bblue");
							$(".label-"+id).addClass("bgreen");
							$(obj).next().html("<?=$labels["bgreen"]?>");
							$(".label-"+id).html("<?=$labels["bgreen"]?>");
							changelabel(id,"bgreen");
						}else if(_class.indexOf("bgreen")>=0){
							$(this).removeClass("bgreen");
							$(this).addClass("bpurple");
							$(".label-"+id).removeClass("bgreen");
							$(".label-"+id).addClass("bpurple");
							$(obj).next().html("<?=$labels["bpurple"]?>");
							$(".label-"+id).html("<?=$labels["bpurple"]?>");
							changelabel(id,"bpurple");
						}else if(_class.indexOf("bpurple")>=0){
							$(this).removeClass("bpurple");
							$(this).addClass("bnone");
							$(".label-"+id).removeClass("bpurple");
							$(obj).next().html("<?=$labels["bnone"]?>");
							$(".label-"+id).html("<?=$labels["bnone"]?>");
							changelabel(id,"bnone");
						}else{
							$(this).removeClass("bnone");
							$(this).addClass("bred");
							$(".label-"+id).addClass("bred");
							$(obj).next().html("<?=$labels["bred"]?>");
							$(".label-"+id).html("<?=$labels["bred"]?>");
							changelabel(id,"bred");
						}
					}
				);
				function changelabel(id,label){
					var surl="../Controller/Navigation.php";
					var sdata={
						"controller":"_ContactController",
						"action":"changelabel",
						"key": id,
						"label": label
					};
					jQuery.ajax({
						type:"POST", 
						data:sdata, 
						dataType:"text", 
						url:surl, 
						success: function (data){ 
							
						},
						error: function (xhr, ajaxOptions, thrownError){
							//$("#errmsg").html("<li><b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError+"</li>");
							//$(".system_messages").show();
						}
					});	
				}
				$(".details").click(
					function(){
						var id=$(this).attr("contactid");
						var _class=$(".data-row.rowid-"+id).attr("class");
						if(_class.indexOf("viewingtop")>=0){
							$(".viewingtop").removeClass("bol");
							$(".viewingtop").removeClass("viewingtop");
							$(".viewingbottom").hide();
							return;	
						}
						$(".viewingtop").removeClass("bol");
						$(".viewingtop").removeClass("viewingtop");
						$(".viewingbottom").hide();
						
						$(".data-row.rowid-"+id).addClass("viewingtop");
						$(".data-row.rowid-"+id).addClass("bol");
						$(".viewingbottom.rowdetail-"+id).show();
						if(_class.indexOf("_new")>=0){
							$(".data-row.rowid-"+id).removeClass("_new");
							$(".data-row.rowid-"+id).addClass("bol");
							var surl="../Controller/Navigation.php";
							var sdata={
								"controller":"_ContactController",
								"action":"viewContact",
								"key": id
							};
							jQuery.ajax({
								type:"POST", 
								data:sdata, 
								dataType:"text", 
								url:surl, 
								success: function (data){ 
									
								},
								error: function (xhr, ajaxOptions, thrownError){
									//$("#errmsg").html("<li><b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError+"</li>");
									//$(".system_messages").show();
								}
							});	
						}
					}
				);
				$(".delete").click(
						function(){
							
							var id=$(this).attr("title");
							var surl="../Controller/Navigation.php";
							var sdata={
								"controller":"_ContactController",
								"action":"deletecontact",
								"key": id
							};
							jQuery.ajax({
								type:"POST", 
								data:sdata, 
								dataType:"text", 
								url:surl, 
								success: function (data){ 
								$("#main_error").append(data);
								},
								error: function (xhr, ajaxOptions, thrownError){
									$("#main_error").append("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError);
								}
							});	
							
						}
					);
				$(".undelete").click(
						function(){
							
							var id=$(this).attr("title");
							var surl="../Controller/Navigation.php";
							var sdata={
								"controller":"_ContactController",
								"action":"restorecontact",
								"key": id
							};
							jQuery.ajax({
								type:"POST", 
								data:sdata, 
								dataType:"text", 
								url:surl, 
								success: function (data){ 
								$("#main_error").append(data);
								},
								error: function (xhr, ajaxOptions, thrownError){
									$("#main_error").append("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError);
								}
							});	
							
						}
					);
			</script>
		</div>
		<?php 
		}
		?>
			
	</div>
	<!--[if !IE]>end section content<![endif]-->
</div>
<!--[if !IE]>end section<![endif]-->
<?php 
if($admin_author===FALSE && $contac_author===False){
	
}else{
?>
<!--[if !IE]>start pagination<![endif]-->
<div class="pagination">
	<span class="page_no"><strong>Results <?=($page-1)*$n+1?>-<?=($page-1)*$n+count($data)?> of <?=$numrow?></strong> | Page <?=$page?>/<?=$numpage?></span>
	
	<ul class="pag_list">
		<li><a href="#" class="button pag_ends first"><span><span>First</span></span></a> </li>
		<li><a href="#" class="button pag_btn prev"><span><span>Previous</span></span></a> </li>
		<?
		if($page>5){
			echo "
			<li><a href='?page=1' class='button pag_btn'><span><span>1</span></span></a></li>
			<li class='gap'>[...]</li>
			";
		}
		if($numpage>0)for($i=$page-4;$i<=$page+4;$i++){
			if($i>0 && $i<=$numpage){
				echo "<li><a href='contacttool.php?page=$i' ";
				if($page==$i){
					echo "<li><a href='javascript:void(0);' class='button pag_on'><span><span>$i</span></span></a></li>";
				}else{
					echo "<li><a href='?page=$i' class='button pag_btn'><span><span>$i</span></span></a></li>";
				}
				echo "";
			}
		}
		if($i<$numpage){
			echo "
			<li class='gap'>[...]</li>
			<li><a href='?page=$numpage' class='button pag_btn'><span><span>$numpage</span></span></a></li>
			";
		}
		?>
		
		<li><a href="#" class="button pag_btn next"><span><span>Next</span></span></a> </li>
		<li><a href="#" class="button pag_ends last"><span><span>Last</span></span></a> </li>
	</ul>
	
	
</div>
<!--[if !IE]>end pagination<![endif]-->
<?php 
}
function emptydata(){
?>
<p>
			<strong style="color:red;">Please login width Admin authority</strong>
		</p>
		<div  class="product_list">
			<!--[if !IE]>start table_wrapper<![endif]-->
			<div class="table_wrapper">
				<div class="table_wrapper_inner">
				<table cellpadding="0" cellspacing="0" width="100%">
					<tbody>
					<tr>
						<th class="no"><span><span><a href="#">No.</a></span></span></th>
						<th class="photo"><span><span>Name</span></span></th>
						<th class="name order"><span><span><a href="#">Email</a> <a href="#" class="desc">descendent order</a></span></span></th>
						<th class="price"><span><span><a href="#">Type</a></span></span></th>
						<th class="insert"><span><span><a href="#">Insert</a></span></span></th>
						<th class="move"><span><span>Move</span></span></th>
						<th class="actions"><span><span>Actions</span></span></th>
					</tr>
					<?
					for($i=1;$i<6;$i++){
					?>
					<tr class="data-row">
						<td style="width: 21px;">
							&nbsp;
						</td>
						<td >
							&nbsp;
						</td>
						<td style="">
							&nbsp;
						</td>
						<td>
							&nbsp;
						</td>
						<td style="">
							&nbsp;
						</td>
						<td style="width: 64px;">
							<div class="move_menu">
								<ul>
									<li><a href="#" class="move1">1</a></li>
									<li><a href="#" class="move2">2</a></li>
									<li><a href="#" class="move3">3</a></li>
									<li><a href="#" class="move4">4</a></li>
								</ul>
							</div>
						</td>
						<td style="width: 120px;">
							<div class="actions_menu">
								<ul>
									<li><a class="details"  href="#">Details</a></li>
									<li><a class="edit" href="#">Edit</a></li>
									<li><a class="delete" href="#">Delete</a></li>
								</ul>
							</div>
						</td>
					</tr>
					<?
					}
					?>
					
					
				</tbody></table>
				</div>
			</div>
			<!--[if !IE]>end table_wrapper<![endif]-->
			
			
		</div>
<?php 
}

?>
